<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	<title></title>
	<link href="./public/admin/Flat/dist/css/vendor/bootstrap.min.css" rel="stylesheet">
	    <!-- Loading Flat UI -->
	<link href="./public/admin/Flat/dist/css/flat-ui.css" rel="stylesheet">
	<link href="./public/admin/Flat/docs/assets/css/demo.css" rel="stylesheet">
	<link rel="shortcut icon" href="./public/admin/Flat/img/favicon.ico">
	<script src="./public/jquery-1.7.2.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="./public/uploadify/jquery.uploadify.min.js" type="text/javascript" charset="utf-8"></script>
	<link rel="stylesheet" type="text/css" href="./public/uploadify/uploadify.css"/>	
	<script type="text/javascript" charset="utf-8" src="Public/ueditor1_4_3/ueditor.config.js"></script>
 	<script type="text/javascript" charset="utf-8" src="Public/ueditor1_4_3/ueditor.all.min.js"> </script>

	<script type="text/javascript">
	
		$(function(){
//			alert();
/*		<option value="{{$v['cid']}}" tid="{{$v['type_tid']}}">{{$v['cname']}}</option>*/
			$('select[name=category_cid]').change(function(){//当name=cid的select的选项被选中时

				var tid=$('option:selected').attr('tid'); //把选中的选项的tid值赋给tid
//			alert();

				if(tid==0){
					alert('是顶级分类，不能添加商品');
				}
//				alert();
//				tid不为0时，执行ajax 把tid传入方法中处理
				$.ajax({
					type:"post",
					url:"{{U('getTypeAttr')}}",
					data:{tid:tid},
					dataType:'json',
					success:function(phpData){
//						alert(phpData);
						var attr='';
						var spec='';
						$.each(phpData, function(k,v) {
//							alert(v.taname);
							if(v.class==0){
//								判定如果是属性的类型属性数据，就组合后写入属性的盒子中
								attr+='<tr class="info"><td>'+v.taname+'</td>';
								attr+='<td><select name="attr['+v.taid+']" id="">';
								attr+='<option value="">请选择</option>';
								$.each(v.tavalue, function(kk,vv) {
									attr+='<option value="'+vv+'">'+vv+'</option>';
								});
								attr+='</select></td></tr>';
								attr+='<input type="hidden" name="type_tid" id="" value="'+v.type_tid+'" />';
							}else{
//							如果是规格写入规格盒子中
								spec+='<tr class="info"><td>'+v.taname+'</td>';	
								spec+='<td><select name="spec['+v.taid+'][gtvalue][]" id="">';
								spec+='<option value="">请选择</option>';
								$.each(v.tavalue, function(kk,vv) {
									spec+='<option value="'+vv+'">'+vv+'</option>';
								});
								spec+='</select></td><td>附加价格：';
								spec+='<input type="text" name="spec['+v.taid+'][added][]" id="" />';
								spec+='</td><td><span class="btn btn-success add-spec">增加规格</span>';
								spec+='</td></tr>';
							}
	
						});
//						alert(attr);
//						alert(spec);
						$('#attr').html(attr);
						$('#spec').html(spec);
					}
				});
			})
		})
	</script>
	
	<script type="text/javascript">
		$(function(){
			$('.add-spec').live('click',function(){
//				alert(123);
//				获取当前点击的添加按钮的父级的tr对象
				var parentTr=$(this).parents('tr');
				var cloneTr=parentTr.clone();//执行克隆
//				改变按钮的class
				cloneTr.find('.add-spec').removeClass('add-spec btn-success').addClass('remove-spec btn-danger').html('删除规格');
				
				parentTr.after(cloneTr);
			})
		})
	</script>
	
		<!--点击删除节点-->
	<script type="text/javascript">
		$(function(){
			$('.remove-spec').live('click',function(){
//				删除当前点击的按钮的父级的tr节点
				$(this).parents('tr').remove();
			})
		})
	</script>
	

</head>
<body>
	<div class="container-fluid">
	<div class="row-fluid">
	<div class="span12">

	<form action="" method='post' enctype="multipart/form-data">
		<fieldset>
			<legend>编辑商品</legend>
			<table class='table table-bordered table-hover'>
				<thead>
					<tr>
						<th colspan="2" class="btn btn-primary">基本信息</th>
					</tr>
				</thead>
				<tbody>
					<tr class="info">
						<td>所属分类</td>
						<td>
						
							<select name="category_cid">
								<option value="0">-请选择-</option>
								{{foreach from="$cateData" key="$k" value="$v"}}
								<option value="{{$v['cid']}}" tid="{{$v['type_tid']}}" {{if value="$v['cid']==$oldData[0]['category_cid']"}} selected {{endif}}>{{$v['_name']}}</option>
								
								{{endforeach}}
							</select>
						</td>
					</tr>
					<tr class="info">
						<td>所属品牌</td>
						<td>
							<select name="brand_bid">
								<option value="0">-请选择-</option>
									{{foreach from="$brandData" key="$k" value="$v"}}
									<option value="{{$v['bid']}}" {{if value="$v['bid']==$oldData[0]['brand_bid']"}} selected {{endif}}>{{$v['bname']}}</option>
									{{endforeach}}
								
							</select>
						</td>
					</tr>
					<tr class="info">
						<td>商品名称</td>
						<td>
							<input type="text" name='gname' value="{{$oldData[0]['gname']}}"/>
						</td>
					</tr>
					<tr class="info">
						<td>单位</td>
						<td>
							<input type="text" name='unit' value='件'/>
						</td>
					</tr>
					<tr class="info">
						<td>市价场</td>
						<td>
							<input type="text" name='marketprice' value="{{$oldData[0]['marketprice']}}"/>
						</td>
					</tr>
					<tr class="info">
						<td>商城价</td>
						<td>
							<input type="text" name='shopprice' value="{{$oldData[0]['shopprice']}}"/>
						</td>
					</tr>
					<tr class="info">
						<td>点击次数</td>
						<td>
							<input type="text" name='click' value="{{$oldData[0]['click']}}"/>
						</td>
					</tr>
				</tbody>
			</table>

			<p class="btn btn-primary">商品属性</p>
			<table class="table table-bordered table-hover" id='attr' class="info">
				{{foreach from="$oldData" key="$k" value="$v"}}
				<tr class="info">
					{{if value="$v['class']==0"}}
					<td>{{$v['taname']}}</td>
					<td>
						<select name="attr[{{$v['taid']}}]" id="">
							{{foreach from="$v['tavalue']" key="$kk" value="$vv"}}
							<option value="{{$vv}}" {{if value="$vv==$v['ganame']"}} selected {{endif}}>{{$vv}}</option>
							{{endforeach}}
						</select>
					</td>
					{{endif}}
				</tr>
				<input type="hidden" name="type_tid" id="" value="{{$v['type_tid']}}" />
				{{endforeach}}
				
			</table>
			<p class="btn btn-primary">商品规格</p>
			<table class="table table-bordered table-hover" id='spec'>
				{{foreach from="$oldData" key="$k" value="$v"}}
				<tr class="info">
					{{if value="$v['class']==1"}}
					<td>{{$v['taname']}}</td>
						<td>
							<!--spec['+v.taid+'][gtvalue][]-->
							<select name="spec[{{$v['taid']}}][gtvalue][]" id="">
							{{foreach from="$v['tavalue']" key="$kk" value="$vv"}}
							<option value="{{$vv}}" {{if value="$vv==$v['ganame']"}} selected {{endif}}>{{$vv}}</option>
							{{endforeach}}
							</select>
						</td>
						<td>附加价格：
							<input type="text" name="spec[{{$v['taid']}}][added][]" id=""  value="{{$v['added']}}"/>
						</td>
						<td>
							<span class="btn btn-success add-spec">增加规格</span>
						</td>
					{{endif}}	
				</tr>
				{{endforeach}}
				
			</table>
			<table class='table table-bordered'>
				<tr>
					<th colspan="3" class="btn btn-primary">列表图</th>
				</tr>
				<tr class="info">
					<td>上传图片</td>
					<td>
						<input type="file" name='pic' id='pic'/>
					</td>
					<script type="text/javascript">
        $(function() {
            $('#pic').uploadify({
                'formData'     : {//POST数据
                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
                },
                'fileTypeDesc' : '上传文件',//上传描述
                'fileTypeExts' : '*.jpg;*.png',
                'swf'      : '{{__PUBLIC__}}/uploadify/uploadify.swf',
                'uploader' : '{{U('upload')}}',
                'buttonText':'选择文件',
                'fileSizeLimit' : '2000KB',
                'uploadLimit' : 22,//上传文件数
                'width':65,
                'height':25,
                'successTimeout':10,//等待服务器响应时间
                'removeTimeout' : 0.2,//成功显示时间
                'onUploadSuccess' : function(file, data, response) {
					//转为json
                    data=$.parseJSON(data);
                    //获得图片地址
                    var imageUrl = data.url;
                    var li="<li class='imgbox' path='"+data.path+"' url='"+data.url+"'><img src='"+imageUrl+"' style='width:200px;' /><input type='hidden' name='pic' value='"+data.path+"'/><a href='javascript:;' class='clo_img' >X</a></li>";
                    $("#pic-list ul").prepend(li);
                }
            });
        });
    </script>
    <script type="text/javascript">
    	$(function(){
    		$('.clo_img').live('click',function(){
    			$(this).parents('.imgbox').remove();
    		})
    		
    	})
    	
    </script>
					<td id='pic-list'>
						<ul>
							<!--再删除图片时删除对应的li标签-->
							<li class="imgbox">
								<img src='{{$oldData[0]["pic"]}}' style='width:200px;' />
								<input type='hidden' name='pic' value='{{$oldData[0]["pic"]}}'/>
								<a href="javascript:;" class="clo_img" >X</a>
							</li>
						</ul>
						
					</td>
				</tr>
			</table>

			<table class='table table-bordered'>
				<tr>
					<th colspan="3" class="btn btn-primary">商品图册</th>
				</tr>
				<tr class="info">
					<td>上传图片</td>
					<td>
						<input type="file" name='photo' id='photo' />
					</td>
					<script type="text/javascript">
        $(function() {
            $('#photo').uploadify({
                'formData'     : {//POST数据
                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
                },
                'fileTypeDesc' : '上传文件',//上传描述
                'fileTypeExts' : '*.jpg;*.png',
                'swf'      : '{{__PUBLIC__}}/uploadify/uploadify.swf',
                'uploader' : '{{U('upload_2')}}',
                'buttonText':'选择文件',
                'fileSizeLimit' : '2000KB',
                'uploadLimit' : 22,//上传文件数
                'width':65,
                'height':25,
                'successTimeout':10,//等待服务器响应时间
                'removeTimeout' : 0.2,//成功显示时间
                'onUploadSuccess' : function(file, data, response) {
					//转为json
                    data=$.parseJSON(data);
                    //获得图片地址
                    var imageUrl = data.url;
                    var li="<li class='imgbox' path='"+data.path+"' url='"+data.url+"'><img src='"+imageUrl+"' style='width:200px;' /><input type='hidden' name='photo[]' value='"+data.path+"'/><a href='javascript:;' class='clo_img' >X</a></li>";
                   
                    $("#photo-list ul").prepend(li);
                }
            });
        });
    </script>
					
					
					<td id='photo-list'>
						<ul>
							<!--再删除图片时删除对应的li标签-->
							{{foreach from="$oldData[0]['photo']" key="$k" value="$v"}}
							<li class="imgbox">
								<img src='{{$v}}' style='width:200px;' />
								<input type='hidden' name='photo[]' value='{{$v}}'/>
								<a href="javascript:;" class="clo_img" >X</a>
							</li>
							{{endforeach}}
						</ul>
						
					</td>
				</tr>
			</table>

			<table class='table'>
				<tr class="next_show">
					<th class="btn btn-primary">商品详细</th>
				</tr>
				<tr class="info">
					<td>
						<script id="editor" name="intro" type="text/plain" style="width:800px;height:400px; ">{{$oldData[0]['intro']}}</script>
						<script type="text/javascript">
							var ue = UE.getEditor('editor');
						</script>	
					</td>
				</tr>
			</table>

			<table class='table'>
				<tr class="next_show">
					<th class="btn btn-primary">售后服务</th>
				</tr>
				<tr class="info">
					<td>

						<script id="editor1" name="service" type="text/plain" style="width:800px;height:400px; ">{{$oldData[0]['service']}}</script>
						<script type="text/javascript">
							var ue = UE.getEditor('editor1');
						</script>	
					</td>
				</tr>
			</table>
			<input type="hidden" name='gid' value='{{Q('get.gid',0,'intval')}}'/>
			<input type="submit" value="确认添加" class="btn btn-primary btn-block btn-large" />
		</fieldset>
	</form>

	</div>
	</div>
	</div>
</body>
</html>